package org.jash.d2211

import android.app.DatePickerDialog
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import org.jash.d2211.databinding.ActivityMainBinding
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        val calendar = Calendar.getInstance()
        initView(calendar)
        binding.yearMonth.setOnClickListener {
            DatePickerDialog(this)
                .apply {
                    setOnDateSetListener { view, year, month, dayOfMonth ->
                        val calendar = Calendar.getInstance()
                        calendar.set(Calendar.YEAR, year)
                        calendar.set(Calendar.MONTH, month)
                        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
                        initView(calendar)
                    }
                }
                .show()
        }
    }
    fun initView(calendar: Calendar) {
        Day.current = calendar.get(Calendar.DAY_OF_MONTH)
        binding.yearMonth.setText(SimpleDateFormat("yyyy-MM").format(calendar.time))
        calendar.set(Calendar.DAY_OF_WEEK, 2)

        val day = mutableListOf<Day>()
        for (i in 0..<7) {
            day += Day(calendar.get(Calendar.DAY_OF_MONTH))
            Log.d("TAG", "onCreate: " + calendar.get(Calendar.DAY_OF_MONTH))
            calendar.add(Calendar.DAY_OF_YEAR, 1)
        }
        Day.days = day
        binding.days = day
    }
}